package org.amphiprion.rfreplayanalyser.ui.race;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class EventFilterDialog extends JDialog {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.amphiprion.rfreplayanalyser.ui.race.messages"); //$NON-NLS-1$
	private int filter = -1;
	private final JPanel contentPanel = new JPanel();
	private JCheckBox chkBestLap = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkBestLap.text")); //$NON-NLS-1$
	private JCheckBox chkStopToDrive = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkStopToDrive.text")); //$NON-NLS-1$
	private JCheckBox chkDamage = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkDamage.text")); //$NON-NLS-1$
	private JCheckBox chkOvertake = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkOvertake.text")); //$NON-NLS-1$
	private JCheckBox chkPit = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkPit.text")); //$NON-NLS-1$
	private JCheckBox chkContactRisk = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkContactRisk.text")); //$NON-NLS-1$
	private JCheckBox chkStartingLight = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkStartingLight.text")); //$NON-NLS-1$
	private JCheckBox chkOffRoad = new JCheckBox(BUNDLE.getString("EventFilterDialog.chkOffRoad.text")); //$NON-NLS-1$

	/**
	 * Create the dialog.
	 */
	public EventFilterDialog() {
		setTitle(BUNDLE.getString("EventFilterDialog.this.title")); //$NON-NLS-1$
		setBounds(100, 100, 415, 193);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, }));
		{
			contentPanel.add(chkBestLap, "2, 2");
		}
		{
			contentPanel.add(chkStopToDrive, "6, 2");
		}
		{
			contentPanel.add(chkDamage, "10, 2");
		}
		{
			contentPanel.add(chkOvertake, "2, 4");
		}
		{
			contentPanel.add(chkPit, "6, 4");
		}
		{
			contentPanel.add(chkContactRisk, "10, 4");
		}
		{
			contentPanel.add(chkStartingLight, "2, 6");
		}
		{
			contentPanel.add(chkOffRoad, "6, 6");
		}
		{
			JLabel lblNewLabel = new JLabel(BUNDLE.getString("EventFilterDialog.lblNewLabel.text")); //$NON-NLS-1$
			contentPanel.add(lblNewLabel, "2, 8, 9, 1");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						filter = 0;
						if (chkBestLap.isSelected()) {
							filter += RaceFrame.EVENT_FILTER_BEST_LAP;
						}
						if (chkContactRisk.isSelected()) {
							filter += RaceFrame.EVENT_CONTACT_RISK;
						}
						if (chkDamage.isSelected()) {
							filter += RaceFrame.EVENT_FILTER_DAMAGE_LOOSE_PART;
						}
						if (chkOffRoad.isSelected()) {
							filter += RaceFrame.EVENT_OFF_ROAD;
						}
						if (chkOvertake.isSelected()) {
							filter += RaceFrame.EVENT_FILTER_OVERTAKE;
						}
						if (chkPit.isSelected()) {
							filter += RaceFrame.EVENT_FILTER_PIT;
						}
						if (chkStartingLight.isSelected()) {
							filter += RaceFrame.EVENT_STARTING_LIGHT;
						}
						if (chkStopToDrive.isSelected()) {
							filter += RaceFrame.EVENT_FILTER_STOP_TO_DRIVE;
						}
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public static int show(Component parent, int currentFilter) {
		EventFilterDialog dialog = new EventFilterDialog();
		dialog.setModal(true);
		dialog.setLocationRelativeTo(parent);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.chkBestLap.setSelected((currentFilter & RaceFrame.EVENT_FILTER_BEST_LAP) != 0);
		dialog.chkContactRisk.setSelected((currentFilter & RaceFrame.EVENT_CONTACT_RISK) != 0);
		dialog.chkDamage.setSelected((currentFilter & RaceFrame.EVENT_FILTER_DAMAGE_LOOSE_PART) != 0);
		dialog.chkOffRoad.setSelected((currentFilter & RaceFrame.EVENT_OFF_ROAD) != 0);
		dialog.chkOvertake.setSelected((currentFilter & RaceFrame.EVENT_FILTER_OVERTAKE) != 0);
		dialog.chkPit.setSelected((currentFilter & RaceFrame.EVENT_FILTER_PIT) != 0);
		dialog.chkStartingLight.setSelected((currentFilter & RaceFrame.EVENT_STARTING_LIGHT) != 0);
		dialog.chkStopToDrive.setSelected((currentFilter & RaceFrame.EVENT_FILTER_STOP_TO_DRIVE) != 0);
		dialog.setVisible(true);

		return dialog.filter;
	}

}
